#! /usr/bin/perl 

use strict;
use Data::Dumper;
use FindBin '$Bin';
use Net::SMTP;
use Encode qw/from_to/;
use CGI::Pretty qw/:standard *-table *-div/;
use lib "$Bin";

sub send_mail {
    my ($from, $tos, $ccs, $subject, $content) = @_;

    my $smtp = Net::SMTP->new('127.0.0.1');
    $smtp->mail($from);
    foreach (@$tos) { $smtp->to($_); }
    foreach (@$ccs) { $smtp->cc($_); }

    $smtp->data();

    # mail head 
    $smtp->datasend("From: $from\n");
    $smtp->datasend("To: " . join(",", @$tos) . "\n");
    $smtp->datasend("Cc: " . join(",", @$ccs) . "\n");
    $smtp->datasend("Subject: " . $subject . "\n");
    $smtp->datasend("MIME-Version: 1.0\n");
    $smtp->datasend("Content-Type: text/html; charset=utf-8\n\n");

    # mail content
    $smtp->datasend("$content\n");
    $smtp->datasend("\n");

    $smtp->dataend();
    $smtp->quit();
}

sub main {
	print "Test Send Mail to internet\n";
	my $from = "510961649\@qq.com";
	my $to = ["qiuzhenguang\@163.com"];
	my $cc = ["qiuzhenguang\@163.com"];
	my $subject = "abcd";
	my $content = "Hi,";
	$content .= "xxxx";
	my $i;
	for ($i=0;$i<1;$i++)
	{
		send_mail($from, $to, $cc, $subject, $content);
	}
}

foreach (1 .. 1) {
main();
}
